<?php	namespace Common\Model;
use Hdphp\Model\Model;

class Brand extends Model
{
	//数据表
	protected $table = "brand";
	//自动验证
	protected $validate = array(
		array("brand_name","required","品牌名称不能为空",3,3),
	
		array("brand_sort","regexp:/^\d{1,10}$/","排序必须为数字",3,3),
	);
	//自动完成
	protected $auto = array(
		
		array("brand_logo","image","method",3,3),
	);
	public function image()
	{
		if(isset($_POST['brand_logo']))
		{
			return $_POST['brand_logo'];
		}
		//设置上传路径
		$path = "Upload/" . date("ymd");
		//执行上传
		$file = Upload::path($path)->size(2000000)->type("jpg,png")->make();
		if($file)
		{
			//执行缩略
			$path = Image::thumb($file[0]['path'],$path . "/logoThumb_" . $file[0]['basename'],100,50,5);
			return $path;
		}
		return "";
	}
	/**
	 * 添加方法
	 */
	public function store()
	{
		if(!$this->create()) return FALSE;
		//上传错误
		if($_FILES['brand_logo']['error'] != 4)
		{
			if($error = Upload::getError())
			{
				$this->error = $error;
				return false;
			}
		}
		//执行添加
		return $this->add();
	}
	/**
	 * 执行编辑
	 */
	public function edit()
	{
		if(!$this->create()) return FALSE;
		//上传错误
		if(isset($_FILES['brand_logo']) && $_FILES['brand_logo']['error'] != 4)
		{
			if($error = Upload::getError())
			{
				$this->error = $error;
				return false;
			}
		}	
		//执行编辑
		return $this->save();	
	}
}
 
 
